package team.sun.sss.common.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import team.sun.sss.common.domain.AjaxResult;
import team.sun.sss.common.domain.StringUtils;
import team.sun.sss.common.domain.TableDataInfo;
import team.sun.sss.common.entity.NoticeEntity;
import team.sun.sss.common.service.NoticeService;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 公告 信息操作处理
 * 
 * @author ''
 */
@Controller
@RequestMapping("/common/notice")
public class NoticeController extends BaseController
{
    private String baseUrl = "notice/";

    @Autowired
    private NoticeService noticeService;


    @RequestMapping("/view")
    public String toUpdateFamily() {
        return baseUrl+"notice";
    }

    /**
     * 查询公告列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(NoticeEntity notice)
    {
        startPage();
        QueryWrapper<NoticeEntity> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(notice.getNoticeTitle()))
        {
            wrapper.like("notice_title",notice.getNoticeTitle());
        }
        List<NoticeEntity> list = noticeService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 新增公告
     */
    @GetMapping("/add")
    public String add()
    {
        return baseUrl + "/add";
    }

    /**
     * 新增保存公告
     */

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(NoticeEntity notice)
    {
        notice.setCreateTime(new Date());
        return toAjax(noticeService.save(notice));
    }

    /**
     * 修改公告
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") Long noticeId, ModelMap mmap)
    {
        mmap.put("notice", noticeService.getById(noticeId));
        return baseUrl + "/edit";
    }

    /**
     * 修改保存公告
     */

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(NoticeEntity notice)
    {
        return toAjax(noticeService.updateById(notice));
    }

    /**
     * 删除公告
     */

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids[])
    {
        return toAjax(noticeService.removeByIds(Arrays.asList(ids)));
    }
}
